package cn.iocoder.yudao.module.vos.controller.admin.marketingscore;

import cn.iocoder.yudao.module.vos.utils.CdrUtil;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.time.LocalDate;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.vos.controller.admin.marketingscore.vo.*;
import cn.iocoder.yudao.module.vos.dal.dataobject.marketingscore.MarketingScoreDO;
import cn.iocoder.yudao.module.vos.service.marketingscore.admin.MarketingScoreService;

@Tag(name = "管理后台 - 名单评分")
@RestController
@RequestMapping("/vos/marketing-score")
@Validated
public class MarketingScoreController {

    @Resource
    private MarketingScoreService marketingScoreService;

    @PostMapping("/create")
    @Operation(summary = "创建名单评分")
    @PreAuthorize("@ss.hasPermission('vos:marketing-score:create')")
    public CommonResult<String> createMarketingScore(@Valid @RequestBody MarketingScoreSaveReqVO createReqVO) {
        return success(marketingScoreService.createMarketingScore(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新名单评分")
    @PreAuthorize("@ss.hasPermission('vos:marketing-score:update')")
    public CommonResult<Boolean> updateMarketingScore(@Valid @RequestBody MarketingScoreSaveReqVO updateReqVO) {
        marketingScoreService.updateMarketingScore(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除名单评分")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('vos:marketing-score:delete')")
    public CommonResult<Boolean> deleteMarketingScore(@RequestParam("id") String id) {
        marketingScoreService.deleteMarketingScore(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除名单评分")
                @PreAuthorize("@ss.hasPermission('vos:marketing-score:delete')")
    public CommonResult<Boolean> deleteMarketingScoreList(@RequestParam("ids") List<String> ids) {
        marketingScoreService.deleteMarketingScoreListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得名单评分")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('vos:marketing-score:query')")
    public CommonResult<MarketingScoreRespVO> getMarketingScore(@RequestParam("id") String id) {
        MarketingScoreDO marketingScore = marketingScoreService.getMarketingScore(id);
        return success(BeanUtils.toBean(marketingScore, MarketingScoreRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得名单评分分页")
    @PreAuthorize("@ss.hasPermission('vos:marketing-score:query')")
    public CommonResult<PageResult<MarketingScoreRespVO>> getMarketingScorePage(
            @Valid MarketingScorePageReqVO pageReqVO) {

        // —— listDate 兜底：未传则默认昨天（马尼拉时区）——
        if (pageReqVO.getListDate() == null
                || pageReqVO.getListDate().length == 0
                || (pageReqVO.getListDate().length == 1 && pageReqVO.getListDate()[0] == null)
                || (pageReqVO.getListDate().length >= 2 && pageReqVO.getListDate()[0] == null && pageReqVO.getListDate()[1] == null)) {
            LocalDate y = LocalDate.now(CdrUtil.ZONE).minusDays(1);
            pageReqVO.setListDate(new LocalDate[]{y, y});
        }

        PageResult<MarketingScoreDO> pageResult = marketingScoreService.getMarketingScorePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MarketingScoreRespVO.class));
    }
    @GetMapping("/export-excel")
    @Operation(summary = "导出名单评分 Excel")
    @PreAuthorize("@ss.hasPermission('vos:marketing-score:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMarketingScoreExcel(@Valid MarketingScorePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MarketingScoreDO> list = marketingScoreService.getMarketingScorePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "名单评分.xls", "数据", MarketingScoreRespVO.class,
                        BeanUtils.toBean(list, MarketingScoreRespVO.class));
    }

}